/**
* Created by xm on 2018/2/8.
*/

var searchEntities = "";
var search_status = false;
$(function () {
    //var aSearchEntity = viewer.entities._entities._array
    //console.log(321, aSearchEntity)
    //搜索
    var currentPage = "1";
    $(".keyword").on("keyup", function (e) {
        console.log(e)
        var oThis = $(this);
        if (e.keyCode == "13") {
            $(".searchbtn").trigger("click");
        } else {
            var oValue = $.trim(oThis.val());
            if (oValue !== "") {
                $(".Search-Clear").show();
            } else {
                $(".Search-Clear").hide();
            }
        }
    });

    
    $(".searchbtn").on("click", function () {
        //currentPage = "1";
        var oThis = $(this);
        var oValue = $.trim(oThis.prev().val());
        searchEntities = "";
        if (!search_status) {
            if (oValue) {
                $(".Search-Clear").show();
                searchEntities = fnSearchByKeyWord(oValue);

                if (searchEntities.length > 0) {
                    $(".search-foot").show();
                    $(".mCustomScrollBox").show();
                    //fnSearchPage(CurrentPage, searchEntities);
                    $(".search-drop").mCustomScrollbar("update");
                    search_status = true;
                } else {
                    $("#search-box").html("<div class=\"SearchNoData\">未能查找相关信息</div>");
                    $(".search-drop").show();
                    $(".search-foot").hide();
                }
            } else {
                $(".Search-Clear").hide();
                $(".search-drop").hide();
            }
        }
        

    });
    //清空搜素字段
    $(".Search-Clear").on("click", function () {
        $(".keyword").val("");
        $(".Search-Clear").hide();
        $(".search-drop").hide();
        for (var i = 0; i < aSearchEntity.length; i++) {
            aSearchEntity[i].show = aSearchEntity[i].originShow;
            aSearchEntity[i].searchShow = false;
        }
        aSearchEntity = [];
        $("#search-box").html('');
        $(".search-drop .mCustomScrollBox").hide();
        search_status = false
        var k = '-1';
        fnSearchByKeyWord(k)
    });
    //飞行
    $(".search-drop").on("click", ".search-item", function () {
        var oThis = $(this);
        var oType = oThis.attr("data-type");
        var sDatasourceType = oThis.attr("datasource-type");
        var oId = oThis.attr("data-id");
        var oEntity = viewer.entities.getById(oId);
        viewer.flyTo(oEntity);
    });
    //分页点击
    $(".page-box a").on("click", function () {
        var oThis = $(this);
        if (!oThis.hasClass("disabled")) {
            var oIndex = oThis.index();
            var oValue = $.trim($(".keyword").val());
            var allPageNum = Math.ceil(searchEntities.length / 12);
            var pageNum = CurrentPage;
            if (oIndex == "0") {
                pageNum = "1";
            } else if (oIndex == "1") {
                pageNum = (CurrentPage - 1);
            } else if (oIndex == "2") {
                pageNum = (CurrentPage * 1 + 1);
            } else if (oIndex == "3") {
                pageNum = allPageNum;
            }
            CurrentPage = pageNum;
            fnSearchPage(pageNum, searchEntities);
        }
    });
});

//分页处理
function fnSearchPage(pageNum, Datas) {
    var dataLength = Datas.length;
    var pageBtns = $(".page-box a");
    var pageNuberBox = $(".page-record span");
    var allPageNum = Math.ceil(dataLength / 12);
    if (dataLength > 0) {
        $(".search-drop").show();
        pageBtns.removeClass("disabled");
        if (pageNum == 1) {
            pageBtns.eq(0).addClass("disabled");
            pageBtns.eq(1).addClass("disabled")
        }
        if (pageNum == allPageNum) {
            pageBtns.eq(2).addClass("disabled");
            pageBtns.eq(3).addClass("disabled")
        }
        pageNuberBox.eq(0).html(pageNum + "/" + allPageNum);
        pageNuberBox.eq(1).html("共" + dataLength + "条记录");
    } else {
        pageBtns.addClass("disabled");
        $(".search-drop").hide();
    }
}



function fnSearchByKeyWord(sKeyword) {
    var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
    /*用于存储结果的对象*/
    var aResult = [];
    /*单个对象格式
    var aResult = [
    {
    obj: "entity对象",
    according: "那个字段查询到的",
    value: "该字段的全部值",
    type:"point/polygon"
    }
    ]*/
    //var oAllDatasource = viewer.dataSources;
    var arr = viewer.entities._entities._array;
    var num = 0;

    for (var i = 0; i < arr.length; i++) {
        /*该次循环的datasource*/
        var name = arr[i]._name;
        var id = arr[i]._id;
        var oCurResultByName = {};
        if (name && name.indexOf(sKeyword) > -1) {
            num++;
            name = name.replace(sKeyword, "<font class='font-light'>" + sKeyword + "</font>");
            $("#search-box").append('<div class="search-item"  data-id="' + id + '"><div class="result-num">' + num + '</div><div class="result-content"><span class="belong">' + name + '</span></div></div>')

            //显示标签
            mark.showMark(id, true)


            oCurResultByName["obj"] = arr[i];
            oCurResultByName.according = "人员名";
            oCurResultByName.value = arr[i].name;
            oCurResultByName.type = "point";
            oCurResultByName.EntityType = "road";
            aResult.push(oCurResultByName);
        } else if (sKeyword == '-1') {
            //隐藏标签
            mark.showMark(id, false)
        }
    };
    //console.log(77, aResult)
    $(".search-drop").show();

    return aResult;
}